﻿using System;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.SqlConnector;

namespace ApplicationLibrary.Preferences
{
    public partial class GenericPreferencePanel<T> : ApplicationLibrary.Preferences.PreferencePanel where T : new()
    {
        public PreferencesManager<T> Preference { get; private set; }

        private LockManager lockManager;
        public T Preferences = new T();

        public GenericPreferencePanel()
            : base()
        {
            InitializeComponent();
        }

        public GenericPreferencePanel(string appName, Type[] types)
            : base(appName)
        {
            InitializeComponent();

            Preference = new PreferencesManager<T>(appName, types);
            lockManager = new LockManager();
        }

        protected override void OnLoad(EventArgs e)
        {
            // For Design mode
            if (Preference != null)
            {
                Preference.ButtonComputer = ButtonComputer;
                Preference.ButtonUser = ButtonUser;
                Preference.ButtonApplication = ButtonApplication;
                Preference.FillComboBox(PrefList);
                AttachData(Preferences);
            }
            if (PrefList.DropDownItems.Count > 0)
                OnFindData((MyToolStripItem)PrefList.DropDownItems[0]);
            base.OnLoad(e);
        }

        protected override bool OnToolStripButtonClick(ButtonClicked button)
        {
            return Preference.ButtonClick(button);
        }

        protected override bool IsLocked()
        {
            return lockManager.CheckIfLocked(AM_Preferences.Name, lockManager.LockKey);
        }

        protected override void LockRecord()
        {
            lockManager.LockRecord(AM_Preferences.Name, lockManager.LockKey);
        }

        protected override void UnLockRecord()
        {
            lockManager.UnLockRecord(AM_Preferences.Name, lockManager.LockKey);
        }

        protected override void OnFindData(MyToolStripItem tsi)
        {
            lockManager.LockKey = tsi.Text;
            UpdateToolbarIcon(tsi);
            Preferences = Preference.ReadPreference(tsi);
            AttachData(Preferences);
            OnPrepareAuxData();
            Validate();
        }

        protected override void OnNewData()
        {
            Preference.Clear();
            CanRemove = false;
        }

        protected override bool OnSaveData()
        {
            if (!this.Validate())
                return false;

            if (IsNew && Preference.ExistPreference())
            {
                MessageBox.Show("C'e'");
                return false;
            }

            bool bOk = Preference.SavePreference(Preferences);
            if (bOk)
                Preference.FillComboBox(PrefList);

            lockManager.LockKey = Preference.LockKey;
            CanRemove = true;
            return bOk;
        }

        protected override bool OnDeleteData()
        {
            bool bOk = Preference.DeletePreference(Preferences);
            if (bOk)
            {
                Preferences = new T();
                AttachData(Preferences);
                Preference.FillComboBox(PrefList);
            }
            return bOk;
        }
    }
}